﻿#region Header

/*
    This file is part of nDoctor Light

    NDoctor is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    NDoctor is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with NDoctor.  If not, see <http://www.gnu.org/licenses/>.
*/

#endregion Header

namespace Probel.Ldoc.Views.ToolBoxes
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;

    using Probel.Ldoc.Services.Dto;
    using Probel.Ldoc.ViewModels.ToolBoxes;

    /// <summary>
    /// Interaction logic for RecordHistoryView.xaml
    /// </summary>
    public partial class RecordHistoryView : Window
    {
        #region Constructors

        public RecordHistoryView(RecordHistoryViewModel viewModel)
        {
            InitializeComponent();
            this.DataContext = viewModel;
        }

        #endregion Constructors

        #region Properties

        private RecordHistoryViewModel ViewModel
        {
            get { return this.DataContext as RecordHistoryViewModel; }
        }

        #endregion Properties

        #region Methods

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var rtf = this.ViewModel.GetStateRtf();

            if (rtf != null && rtf.Length > 0)
            {
                this.editor.IsEnabled = true;
                this.editor.Document.Blocks.Clear();
                var stream = new MemoryStream(ASCIIEncoding.Default.GetBytes(rtf));
                this.editor.Selection.Load(stream, DataFormats.Rtf);

                //Empty the text selection
                this.editor.Selection.Select(this.editor.Document.ContentEnd, this.editor.Document.ContentEnd);
            }

            else { this.editor.Document.Blocks.Clear(); }
        }

        #endregion Methods
    }
}